package com.enjoyor.soa.traffic.ping.efoms.dubbo;

import java.nio.charset.Charset;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.csvreader.CsvReader;
import com.enjoyor.soa.traffic.ping.efoms.pojo.PingResultPojo;
import com.enjoyor.soa.traffic.util.pojo.ResultPojo;

@Component
public class Query {
	@Value("${ping.times.heigh}")
	private int heighTimes;

	@Value("${ping.tiemout}")
	private int timeout;
	
	@Value("${ping.device.searchFlag}")
	private String searchFlag;
	
	@Value("${ping.device.category}")
	private String category;
	
	@Value("${ping.cvs.path}")
	private String cvsPath;

	public ResultPojo getPingResultPojoList()throws Exception {
		List<PingResultPojo> PingResultPojoList = new ArrayList<>();
		CsvReader csvReader = new CsvReader(cvsPath, ',', Charset.forName("GBK"));
		csvReader.readHeaders();
		if("bayonet".equals(searchFlag)){
		while (csvReader.readRecord()) {
			PingResultPojo pingPojo = new PingResultPojo();
			String OFFSITEID = csvReader.get(0);
			String OFFSITE_NAME = csvReader.get(1);
			String OSSTATUS = csvReader.get(3);
			String IP = csvReader.get(2);
			pingPojo.setDevCategoryCode(category);
			pingPojo.setDevTypeCode("REPDEVTYPE03");
			pingPojo.setDevId(OFFSITEID);
			pingPojo.setDevName(OFFSITE_NAME);
			pingPojo.setDevStatus(OSSTATUS);
			pingPojo.setIP(IP);
			pingPojo.setTimes(heighTimes);
			pingPojo.setTimeout(timeout);
			PingResultPojoList.add(pingPojo);
		}}else if ("other".equals(searchFlag)) {
			while (csvReader.readRecord()) {
			PingResultPojo pingPojo = new PingResultPojo();
			String devId = csvReader.get(0);
			String devName = csvReader.get(1);
			String status = csvReader.get(2);
			String typeCode = csvReader.get(3);
			String IP = csvReader.get(4);
			pingPojo.setDevCategoryCode(category);
			pingPojo.setDevTypeCode(typeCode);
			pingPojo.setDevId(devId);
			pingPojo.setDevName(devName);
			pingPojo.setDevStatus(status);
			pingPojo.setIP(IP);
			pingPojo.setTimes(heighTimes);
			pingPojo.setTimeout(timeout);
			PingResultPojoList.add(pingPojo);
		}}
		ResultPojo resultPojo = new ResultPojo();
		resultPojo.setResultList(PingResultPojoList);
		return resultPojo;
	}
}
